﻿namespace BM.Tools.WebControls.MForm.Controls.Additions
{    
    using System.Collections;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web.UI;

    using Helper;

    /// <summary>
    /// A control that displays all additions
    /// </summary>    
    public class AdditionsDataControl : Control, IEnumerable<Addition>
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="AdditionsDataControl"/> class.
        /// </summary>
        /// <param name="additionsTemplate">
        /// The additions template.
        /// </param>
        public AdditionsDataControl(ITemplate additionsTemplate)
        {
            if (additionsTemplate == null)
            {
                return;
            }

            additionsTemplate.InstantiateIn(this);
        }

        /// <summary>
        /// Gets constraint controls from the collection of all controls.
        /// </summary>
        /// <value>
        /// The constraints.
        /// </value>
        public IEnumerable<Constraint> Constraints
        {
            get
            {
                foreach (var control in Controls)
                {
                    var constraint = control as Constraint;
                    if (constraint != null)
                    {
                        yield return constraint;
                    }
                }
            }
        }

        /// <summary>
        /// Gets or sets a value indicating whether the control is visible.
        /// </summary>
        public override bool Visible
        {
            get
            {
                return !DesignMode && base.Visible;
            }

            set
            {
                base.Visible = value;
            }
        }

        /// <summary>
        /// Returns the Additions enumerator for the <see cref="IEnumerable{Addition}"/> interface
        /// </summary>
        /// <returns>
        /// the enumerator
        /// </returns>
        public IEnumerator<Addition> GetEnumerator()
        {
            var list = new List<Addition>();
            foreach (Control control in Controls)
            {
                var addition = control as Addition;
                if (addition != null)
                {
                    list.Add(addition);
                }
            }

            return list.GetEnumerator();
        }

        /// <summary>
        /// Returns the Additions enumerator for the <see cref="IEnumerable{Addition}"/> interface
        /// </summary>
        /// <returns>
        /// the enumerator
        /// </returns>
        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }

        /// <summary>
        /// add an addition control
        /// </summary>
        /// <param name="addition">
        /// The addition control.
        /// </param>
        public void Add(Addition addition)
        {
            Controls.Add(addition);
        }

        /// <summary>
        /// Renders the control to the specified HTML writer if there is at least one Addition.
        /// </summary>
        /// <param name="writer">The <see cref="T:System.Web.UI.HtmlTextWriter"/> object that receives the control content. </param>
        protected override void Render(HtmlTextWriter writer)
        {
            if (this.Count() == 0)
            {
                return;
            }

            writer.Write("<!--[");
            base.Render(writer);
            writer.Write("]-->");
        }

        /// <summary>
        /// render only the children controls which inherit from the <see cref="Addition"/> type
        /// </summary>
        /// <param name="writer">
        /// the writer
        /// </param>
        protected override void RenderChildren(HtmlTextWriter writer)
        {
            var additions = this.ToArray();
            for (var i = 0; i < additions.Length - 1; i++)
            {
                additions[i].RenderControl(writer);
                writer.Write(",");
            }

            additions[additions.Length - 1].RenderControl(writer);                                                
        }     
    }
}